/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.helper;

import java.util.Iterator;

import org.eclipse.gef.DefaultEditDomain;
import org.eclipse.gef.EditDomain;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Element;

import com.cea.papyrus.core.IPapyrusUIConstants;
import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.editor.DiagramEditorPage;
import com.cea.papyrus.core.editor.DiagramPalette;
import com.cea.papyrus.core.editor.MultiDiagramEditor;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;

/**
 * This class allows researching good factory by taking account the current diagram.
 */
public class ModelerHelper {
	
	/**
	 * debug channel.
	 */
	protected final static int channel = 21;

	/**
	 * 
	 */
	public static String USER_DEFINED_PRESENTATION="UserDefined";
	
	/**
	 * 
	 */
	public static String DEFAULT_PRESENTATION="Default";
	
	/**
	 * 
	 */
	public static String SHORT_LABEL_PRESENTATION="ShortLabel";
	
	/**
	 * 
	 */
	public static String INTERMEDIATE_LABEL_PRESENTATION="IntermediateLabel";
	
	/**
	 * 
	 */
	public static String LONG_LABEL_PRESENTATION="LongLabel";
	
	/**
	 * 
	 */
	public static String TEXT_STEREOTYPE_PRESENTATION="TextStereotype";
	
	/**
	 * 
	 */
	public static String TEXT_ICON_STEREOTYPE_PRESENTATION="TextIconStereotype";
	
	/**
	 * 
	 */
	public static String ICON_STEREOTYPE_PRESENTATION="IconStereotype";
	
	/**
	 * 
	 */
	public static String IMAGE_STEREOTYPE_PRESENTATION="ImageStereotype";
	
	/**
	 * 
	 */
	public static String STEREOTYPE_TEXT_HORIZONTAL_PRESENTATION="HorizontalStereo";
	
	/**
	 * 
	 */
	public static String STEREOTYPE_TEXT_VERTICAL_PRESENTATION="VerticalStereo";
	
	/**
	 * 
	 */
	public static String PROPERTY_STEREOTYPE_DISPLAY="PropStereoDisplay";
	
	/**
	 * 
	 */
	public static String PROPERTY_STEREOTYPE_PROPERTY_VALUES_DISPLAY="PropStereoPropValuesDisplay";
	
	/**
	 * 
	 */
	public static String OPAQUE_EXPRESSION_BODY_DISPLAY="OpaqueExpressionBodyDisplay";

	
	/**
	 * 
	 */
//	private static final IModelerHelper[] diagramFactories = new IModelerHelper[] {
//		ClassDiagramModelerHelper.getInstance(), 
//		CompositeModelerHelper.getInstance(),
//		StateMachineDiagramModelerHelper.getInstance(),
//		UseCaseDiagramModelerHelper.getInstance(),
//		DeploymentDiagramModelerHelper.getInstance(),
//		ActivityDiagramModelerHelper.getInstance(),
//		ComponentDiagramModelerHelper.getInstance()
//		};
	
	/**
	 * returns the good factory by taking into account the current diagram.
	 * @return IModelerFactory the good factory of the current diagram
	 */
	public static IModelerHelper getFactory(){
		//look for current diagram
		//Darpan
		String type = null;
		IEditorPart ge = (IEditorPart) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		if (ge instanceof MultiDiagramEditor) {
			MultiDiagramEditor mde = (MultiDiagramEditor) ge;
			DiagramEditorPage cde = (DiagramEditorPage) mde.getActiveEditor();
			if (cde == null) {
				// No opened diagram, use ClassDiagramModelerFactory
				Debug.error(null, "No openedDiagram", channel);
				return getModelHelperFactory().getFactory(IPapyrusUIConstants.CLASS_DIAGRAM);
			}
			Diagram dia = cde.getDiagramModel();
			type = dia.getType();
		} else if (ge instanceof GraphicalEditorWithFlyoutPalette) {
			DefaultEditDomain domain = (DefaultEditDomain) 
				((GraphicalEditorWithFlyoutPalette) ge).getAdapter(EditDomain.class);
			PaletteRoot pRoot = domain.getPaletteViewer().getPaletteRoot();
			boolean found  = false;
			for (Object aChildPalette : pRoot.getChildren()) {
				if (aChildPalette instanceof DiagramPalette) {
					type = ((DiagramPalette) aChildPalette).getTypeDiagram();
					found = true;
				}
			}
			if (!found) {
				// if no palette is found, default to class diagram palette
				type = IPapyrusUIConstants.CLASS_DIAGRAM;
			}
		}
		return getFactory(type);
		//Darpan
	}
	
	/**
	 * returns the  factory associated to the given diagram type
	 * @param type the type of current diagram
	 * @return IModelerFactory the factory for the specified diagram type
	 */
	public static IModelerHelper getFactory(String type) {
		//research good factory by taking account the diagramtype
		return getModelHelperFactory().getFactory(type);
//		if (TypeDiagram.CLASS_DIAGRAM.equals(type)){
//			return ClassDiagramModelerHelper.getInstance();
//		} else if (TypeDiagram.COMPOSITE_STRUCTURE_DIAGRAM.equals(type)){
//			return CompositeModelerHelper.getInstance();
//		} else if (TypeDiagram.STATE_MACHINE_DIAGRAM.equals(type)){
//			return StateMachineDiagramModelerHelper.getInstance();
//		} else if (TypeDiagram.USE_CASE_DIAGRAM.equals(type)){
//			return UseCaseDiagramModelerHelper.getInstance();
//		} else if (TypeDiagram.DEPLOYMENT_DIAGRAM.equals(type)){
//			return DeploymentDiagramModelerHelper.getInstance();
//		} else if (TypeDiagram.ACTIVITY_DIAGRAM.equals(type)){
//			return ActivityDiagramModelerHelper.getInstance();
//		} else if (TypeDiagram.COMPONENT_DIAGRAM.equals(type)){
//			return ComponentDiagramModelerHelper.getInstance();			
//		}// else return default factory
//		return ClassDiagramModelerHelper.getInstance();
	}

	/**
	 * Get the model helper factory.
	 * For now, get it from the plugin main class.
	 * @return
	 */
	private static ModelerHelperRegistry getModelHelperFactory() {
		return PapyrusPlugin.getDefault().getFactoryRegistry();
	}

	/**
	 * 
	 * 
	 * @param elementClass 
	 * @param parent 
	 * 
	 * @return 
	 */
	public static Element createElement(Class elementClass, Element parent) {
		Element element = null;

		// iterate over Diagram Factories and try to find the factory that is aware of elementClass.
		Iterator<IModelerHelper> iter = getModelHelperFactory().getFactories().iterator();
		while( iter.hasNext() )
		{
			IModelerHelper factory = iter.next();
			element = factory.createUMLElement(elementClass, parent);
			if( element != null)
				return element;
		}
		
		// return null (not found)
		return element;
	}
	
	/**
	 * 
	 * 
	 * @param element 
	 */
	public static void deleteElement(Element element) {
//		Iterator<IModelerHelper> iter = getModelHelperFactory().getFactories().iterator();
//		while( iter.hasNext() )
//		{
//			IModelerHelper factory = iter.next();
//		}
		
	}
	
}
